import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lang/flutter_lang.dart';
import 'package:flutter_lifecycle_base/tab/base_tab_widget.dart';
import 'package:flutter_lifecycle_base/tab/pageview_widget.dart';
import 'package:flutter_lifecycle_base/widget/status_bar_wrap.dart';
import 'package:wallet_home/image_r.dart';
import 'package:wallet_home/page/main_page/main_page_my.dart';
import 'package:wallet_home/page/main_page/main_page_wallet.dart';
import 'package:wallet_home/page/theme/colors.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'package:wallet_home/widget/coins_manager.dart';
import 'package:wallet_home/widget/main_page_drawer_widget.dart';

import '../../main_double_back_tap_exit_app.dart';
import '../../module_config.dart';

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

class MainBottomTabView extends BaseTabWidget {
  @override
  _MainBottomTabViewState createState() => _MainBottomTabViewState();
}

class _MainBottomTabViewState extends BaseTabState<MainBottomTabView> {
  int _currentIndex = 0;
  List<Widget> _pages;
  PageController _controller;

  List<Widget> pages = [
    MainPageWalletWidget(key: walletPageKey, walletKey: _scaffoldKey),
    MainPageMyWidget(),
  ];

  List<BottomNavigationBarItem> tabList;

  @override
  void initState() {
    super.initState();
    _currentIndex = 0;
    _pages = pages;
    _controller = PageController(keepPage: true);
    print("init=======>home");
  }

  void getTabList(BuildContext context) {
    tabList = [
      BottomNavigationBarItem(
          icon: Image.asset(ImageR.imagesWalletIconNor,
              width: 25,
              height: 20,
              fit: BoxFit.fill,
              color: ThemeUtils.getThemeColor(
                  context: context,
                  brightnessColor: Colors.grey[400],
                  darkColor: Colors.grey[500]),
              package: ModuleConfig.moduleName),
          title: Container(
              //margin: EdgeInsets.only(top: 5),
              padding: EdgeInsets.only(top: 3),
              child: Text(
                "${WXLang.$t("home_wallet",package: ModuleConfig.moduleName)}",
              )),
          activeIcon: Image.asset(ImageR.imagesWalletIconSel,
              width: 25,
              height: 20,
              fit: BoxFit.fill,
              color: ThemeUtils.getThemeColor(
                  context: context,
                  brightnessColor: Colors.grey[800],
                  darkColor: Colors.white),
              package: ModuleConfig.moduleName)),
      BottomNavigationBarItem(
        icon: Image.asset(ImageR.imagesMineIconNor,
            width: 25,
            height: 25,
            fit: BoxFit.fill,
            color: ThemeUtils.getThemeColor(
                context: context,
                brightnessColor: Colors.grey[400],
                darkColor: Colors.grey[500]),
            package: ModuleConfig.moduleName),
        title: Text(
          "${WXLang.$t("home_mine",package: ModuleConfig.moduleName)}",
        ),
        activeIcon: Image.asset(ImageR.imagesMineIconSel,
            width: 25,
            height: 25,
            fit: BoxFit.fill,
            color: ThemeUtils.getThemeColor(
                context: context,
                brightnessColor: Colors.grey[800],
                darkColor: Colors.white),
            package: ModuleConfig.moduleName),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    getTabList(context);
    return DoubleBackTapExitApp(
      child: Scaffold(
          key: _scaffoldKey,
          body: MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: PageWidget(
                /// 禁止滑动
                physics: NeverScrollableScrollPhysics(),
                tabWidgets: _pages,
                controller: _controller,
                onPageChanged: _onPageChanged,
              )),
          bottomNavigationBar: SafeArea(
            bottom: false,
            child: Container(
              height: MediaQuery.of(context).padding.bottom > 0 ? 88 : 56,
              decoration: BoxDecoration(
                  color: Colors.red,
                  border: Border(
                      top: BorderSide(color: Colors.grey[200], width: 0.1))),
              width: double.infinity,
              child: BottomNavigationBar(
                items: tabList,
                currentIndex: _currentIndex,
                onTap: _onTabChanged,
                selectedItemColor: ThemeUtils.getThemeColor(
                    context: context,
                    brightnessColor: Colors.grey[800],
                    darkColor: Colors.white),
                unselectedItemColor: ThemeUtils.getThemeColor(
                    context: context,
                    brightnessColor: Colors.grey[400],
                    darkColor: Colors.grey[500]),
                selectedFontSize: 12,
                unselectedFontSize: 12,
              ),
            ),
          ),
          drawer: MainPageDrawerWidget()),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  void didAppear() {
    super.didAppear();
  }

  @override
  void disappear() {
    super.disappear();
  }

  void _onPageChanged(int index) {}

  void _onTabChanged(int index) {
    jumpToIndex(index);
  }

  /// 跳到指定的Tab
  void jumpToIndex(int index) {
    setState(() {
      if (_currentIndex != index) {
        _currentIndex = index;
        _controller.jumpToPage(_currentIndex);
      }
    });
  }
}
